package 课后作业1.Test1;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestStudent {
    static List<Student> studentList=new ArrayList<>();

    public void StuSystem(){
        Scanner in=new Scanner(System.in);
        boolean b=true;
        while (b) {
            System.out.println("请选择您的操作");
            System.out.println("1、显示全部学员\n"+"2、增加\n" + "3、删除\n" + "4、修改\n" + "0、退出");
            int i=in.nextInt();
            switch (i){
                case 1:{
                    showStudent();
                    break;
                } case 2:{
                    System.out.println("请输入学生学号：");
                    String num=in.next();
                    System.out.println("请输入学生姓名：");
                    String name=in.next();
                    System.out.println("请输入学生年龄：");
                    int age=in.nextInt();
                    System.out.println("请输入学生身份证：");
                    String id=in.next();
                    Student student=new Student(num,age,name,id);
                    System.out.println(addStudent(student));
                    break;
                } case 3:{
                    System.out.println("请输入要删除的学生学号:");
                    String num=in.next();
                    boolean delete=deleteStudent(num);
                    if(delete){
                        System.out.println("删除成功");
                    }else {
                        System.out.println("删除失败");
                    }
                    break;
                } case 4:{
                    System.out.println("请输入要修改的学生学号:");
                    String num=in.next();
                    boolean update=updateStudent(num);
                    if(update){
                        System.out.println("修改成功");
                    }else {
                        System.out.println("未找到该学生");
                    }
                    break;
                } case 0:{
                    System.out.println("感谢使用");
                    System.exit(0);
                }
            }
            System.out.println("是否继续操作（输入y继续）：");
            String s=in.next();
            if(!("y".equalsIgnoreCase(s))){
                b=false;
            }
        }
    }

    /**
     * 添加学生
     * @param student 要添加的学生
     * @return 是否成功
     */
    public String addStudent(Student student){
        for (Student student1:studentList){
            if(student1.equals(student)){
                return "该学号已存在";
            }
        }
        studentList.add(student);
        return "添加成功";
    }

    /**
     * 显示全部学员
     */
    public void showStudent(){
        System.out.println("学号\t姓名\t年龄\t身份证");
        for (Student student:studentList){
            System.out.println(student.getStuNum()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getIdCard());
        }
    }

    /**
     * 删除学生
     * @param num 要删除的学生学号
     * @return 是否删除成功s
     */
    public boolean deleteStudent(String num){
        for (Student student:studentList){
            if(num.equals(student.getStuNum())){
                studentList.remove(student);
                return true;
            }
        }
        return false;
    }

    /**
     * 修改学生信息
     * @param num 要修改的学生学号
     * @return 是否修改成功
     */
    public boolean updateStudent(String num){
        Scanner in=new Scanner(System.in);
        for (Student student:studentList){
            if(num.equals(student.getStuNum())){
                System.out.println("请输入姓名：");
                String name=in.next();
                System.out.println("请输入年龄：");
                int age=in.nextInt();
                System.out.println("请输入身份证：");
                String id=in.next();
                Student newStudent=new Student(num,age,name,id);
                studentList.remove(newStudent);
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        TestStudent t=new TestStudent();
        t.StuSystem();
    }
}
